package com.example.msdksample;

import android.app.Application;
import android.content.Context;
import android.util.Log;
import dji.v5.common.error.IDJIError;
import dji.v5.common.register.DJISDKInitEvent;
import dji.v5.manager.SDKManager;
import dji.v5.manager.interfaces.SDKManagerCallback;

public class MyApplication extends Application {

    private final String TAG = MyApplication.class.getSimpleName();
    private LogCallback logCallback;

    public interface LogCallback {
        void onLogReceived(String logMessage);
    }

    public void setLogCallback(LogCallback callback) {
        this.logCallback = callback;
    }

    private void appendLog(String message) {
        Log.i(TAG, message);
        if (logCallback != null) {
            logCallback.onLogReceived(message);
        }
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        // 在调用install前，请勿调用任何MSDK相关
        // MSDK v5.10.0 之前的版本请使用 com.secneo.sdk.Helper.install(this)
        com.cySdkyc.clx.Helper.install(this);
        appendLog("SDK Helper installed");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        appendLog("Application onCreate started");

        // 初始化MSDK，建议初始化逻辑放在Application中，当然也可以根据自己的需要放在任意地方。
        SDKManager.getInstance().init(this, new SDKManagerCallback() {
            @Override
            public void onInitProcess(DJISDKInitEvent event, int totalProcess) {
                String message = "onInitProcess: " + (event != null ? event.name() : "null") + ", totalProcess: " + totalProcess;
                appendLog(message);
                if (event == DJISDKInitEvent.INITIALIZE_COMPLETE) {
                    appendLog("Initialization complete, registering app...");
                    SDKManager.getInstance().registerApp();
                }
            }

            @Override
            public void onRegisterSuccess() {
                appendLog("onRegisterSuccess: SDK registered successfully");
            }

            @Override
            public void onRegisterFailure(IDJIError error) {
                String errorMsg = "onRegisterFailure: " + (error != null ? error.description() : "Unknown error");
                appendLog(errorMsg);
            }

            @Override
            public void onProductConnect(int productId) {
                appendLog("onProductConnect: Product connected, ID: " + productId);
            }

            @Override
            public void onProductDisconnect(int productId) {
                appendLog("onProductDisconnect: Product disconnected, ID: " + productId);
            }

            @Override
            public void onProductChanged(int productId) {
                appendLog("onProductChanged: Product changed, ID: " + productId);
            }

            @Override
            public void onDatabaseDownloadProgress(long current, long total) {
                if (total > 0) {
                    int progress = (int) ((current * 100) / total);
                    appendLog("onDatabaseDownloadProgress: " + progress + "% (" + current + "/" + total + ")");
                }
            }
        });
    }
}